package com.meirengu.webview.service.impl;

import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.webview.dao.KwChannelDataDao;
import com.meirengu.webview.dao.KwTrackEventDao;
import com.meirengu.webview.dao.KwTrackEventInfoDao;
import com.meirengu.webview.dao.UserDao;
import com.meirengu.webview.model.KwChannel;
import com.meirengu.webview.model.KwTrackEvent;
import com.meirengu.webview.model.KwTrackEventInfo;
import com.meirengu.webview.service.KwChannelService;
import com.meirengu.webview.service.KwTrackEventService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * KwTrackEvent服务实现层
 *
 * @author Mao
 * @create Fri Apr 13 17:09:53 CST 2018
 */
@Service
public class KwTrackEventServiceImpl extends BaseServiceImpl<KwTrackEvent> implements KwTrackEventService {

    @Autowired
    KwTrackEventDao kwTrackEventDao;

    @Autowired
    KwTrackEventInfoDao kwTrackEventInfoDao;
    @Autowired
    UserDao userDao;
    @Autowired
    KwChannelDataDao kwChannelDataDao;
    @Autowired
    KwChannelService kwChannelService;

    /**
     * 事件跟踪
     *
     * @param id
     * @throws Exception
     */
    public void trackEvent(String id, String mobile, String ip, String cid) throws Exception {
        //查询当日记录
        Date currentDate = new Date();
        //当天开始时间
        Date dayBeginTime = DateUtils.getDayBeginTime(currentDate);
        //当天结束时间
        Date dayEndTime = DateUtils.getDayEndTime(currentDate);
        Map<String, Object> map = new HashMap<>();
        map.put("label", id);
        map.put("dataDateBegin", dayBeginTime);
        map.put("dataDateEnd", dayEndTime);
        if (StringUtils.isNotBlank(cid) && cid.contains("#")) {
            int endIndex = cid.indexOf("#");
            cid = cid.substring(0, endIndex);
        }
        List<Map<String, Object>> trackEventList = getList(map);
        if (trackEventList != null && trackEventList.size() > 0) {
            for (Map<String, Object> eventMap : trackEventList) {
                if (StringUtils.isNotBlank(eventMap.get("id").toString())) {
                    kwTrackEventDao.updatePv(eventMap.get("id").toString());
                    String category = eventMap.get("category").toString();
                    String action = eventMap.get("action").toString();
                    String label = eventMap.get("label").toString();
                    KwTrackEventInfo kwTrackEventInfo = new KwTrackEventInfo();
                    kwTrackEventInfo.setCategory(category);
                    kwTrackEventInfo.setAction(action);
                    kwTrackEventInfo.setLabel(label);
                    kwTrackEventInfo.setMobile(mobile);
                    kwTrackEventInfo.setIp(ip);
                    kwTrackEventInfo.setChannelCode(cid);
                    kwTrackEventInfoDao.insert(kwTrackEventInfo);
                }
            }
        }
        KwChannel kwChannel = kwChannelService.getByCode(cid);
        // 查出用户渠道并统计到渠道数据表中
        if (kwChannel != null && StringUtils.isNotBlank(kwChannel.getId())) {
            Map<String, String> paramsMap = new HashMap<>();
            paramsMap.put("channelId", kwChannel.getId());
            paramsMap.put("productNum", "1");
            kwChannelDataDao.addNum(paramsMap);

        }
    }
}
